<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="项目名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入项目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地区" prop="city">
        <el-input
          v-model="queryParams.city"
          placeholder="请输入地区"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="县城" prop="county">
        <el-input
          v-model="queryParams.county"
          placeholder="请输入县城"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="备案时间" prop="recordTime">-->
<!--        <el-input-->
<!--          v-model="queryParams.recordTime"-->
<!--          placeholder="请输入备案时间"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="投决备案规模" prop="scale">-->
<!--        <el-input-->
<!--          v-model="queryParams.scale"-->
<!--          placeholder="请输入投决备案规模"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="投决备案时间" prop="scaleTime">-->
<!--        <el-input-->
<!--          v-model="queryParams.scaleTime"-->
<!--          placeholder="请输入投决备案时间"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="开工时间" prop="startTime">-->
<!--        <el-input-->
<!--          v-model="queryParams.startTime"-->
<!--          placeholder="请输入开工时间"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="投产时间" prop="operationTime">-->
<!--        <el-input-->
<!--          v-model="queryParams.operationTime"-->
<!--          placeholder="请输入投产时间"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="投产规模" prop="operationScale">-->
<!--        <el-input-->
<!--          v-model="queryParams.operationScale"-->
<!--          placeholder="请输入投产规模"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="经纬度" prop="lonLat">-->
<!--        <el-input-->
<!--          v-model="queryParams.lonLat"-->
<!--          placeholder="请输入经纬度"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="排序字段" prop="sort">-->
<!--        <el-input-->
<!--          v-model="queryParams.sort"-->
<!--          placeholder="请输入排序字段"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:appProject:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:appProject:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:appProject:remove']"
        >删除</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['system:appProject:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="appProjectList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="项目列表" align="center" prop="id" />-->
      <el-table-column label="项目名称" align="center" prop="name" />
      <el-table-column label="地区" align="center" prop="city" />
      <el-table-column label="县城" align="center" prop="county" />
      <el-table-column label="备案时间" align="center" prop="recordTime" />
      <el-table-column label="投决备案规模" align="center" prop="scale" />
      <el-table-column label="投决备案时间" align="center" prop="scaleTime" />
      <el-table-column label="开工时间" align="center" prop="startTime" />
      <el-table-column label="投产时间" align="center" prop="operationTime" />
      <el-table-column label="投产规模" align="center" prop="operationScale" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="经纬度" align="center" prop="lonLat" />
      <el-table-column label="类型" align="center" prop="type">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.type == 1">风力</el-tag>
          <el-tag v-if="scope.row.type == 2">光伏</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="排序字段" align="center" prop="sort" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:appProject:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:appProject:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改项目列表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="70%" append-to-body>

      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="项目名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入项目名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="类型" prop="type">
              <el-select v-model="form.type" placeholder="请选择类型">
                <el-option label="风力" value="1" />
                <el-option label="光伏" value="2" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="地区" prop="city">
              <el-input v-model="form.city" placeholder="请输入地区" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="县城" prop="county">
              <el-input v-model="form.county" placeholder="请输入县城" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备案时间" prop="recordTime">
              <el-input v-model="form.recordTime" placeholder="请输入备案时间" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="投决备案规模" prop="scale">
              <el-input v-model="form.scale" placeholder="请输入投决备案规模" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="投决备案时间" prop="scaleTime">
              <el-input v-model="form.scaleTime" placeholder="请输入投决备案时间" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开工时间" prop="startTime">
              <el-input v-model="form.startTime" placeholder="请输入开工时间" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="投产时间" prop="operationTime">
              <el-input v-model="form.operationTime" placeholder="请输入投产时间" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="投产规模" prop="operationScale">
              <el-input v-model="form.operationScale" placeholder="请输入投产规模" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="排序字段" prop="sort">
              <el-input v-model="form.sort" placeholder="请输入排序字段" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="经纬度" prop="lonLat">
              <el-input v-model="form.lonLat" placeholder="请输入经纬度" readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="24" v-if="open">
            <el-form-item label="地图">
              <LatitudeAndLongitude :defaultPoint="point"
                                    @sendLoction="getLoction"></LatitudeAndLongitude>
            </el-form-item>
          </el-col>
        </el-row>













      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAppProject, getAppProject, delAppProject, addAppProject, updateAppProject } from "@/api/system/appProject";
import LatitudeAndLongitude from "@/components/LatitudeAndLongitude/index.vue";

export default {
  name: "AppProject",
  components: {LatitudeAndLongitude},
  data() {
    return {
      point:['106.26667','38.46667'],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目列表表格数据
      appProjectList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        city: null,
        county: null,
        recordTime: null,
        scale: null,
        scaleTime: null,
        startTime: null,
        operationTime: null,
        operationScale: null,
        lonLat: null,
        type: null,
        sort: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        type : [
          { required: true, message: "类型不能为空", trigger: "blur" }
        ],
        lonLat : [
          { required: true, message: "经纬度不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getLoction(point, address) {
      this.form.lonLat = point[0] + ',' + point[1];
      // this.form.longitude = point[0];
      // this.form.latitude = point[1];
      // this.form.detailedAddress = address;
    },
    /** 查询项目列表列表 */
    getList() {
      this.loading = true;
      listAppProject(this.queryParams).then(response => {
        this.appProjectList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        city: null,
        county: null,
        recordTime: null,
        scale: null,
        scaleTime: null,
        startTime: null,
        operationTime: null,
        operationScale: null,
        remark: null,
        lonLat: null,
        type: null,
        sort: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加项目列表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAppProject(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改项目列表";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAppProject(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAppProject(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除项目列表编号为"' + ids + '"的数据项？').then(function() {
        return delAppProject(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/appProject/export', {
        ...this.queryParams
      }, `appProject_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
